package landun.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import landun.pojo.SysEmployee;
import landun.pojo.SysPosition;
import landun.service.EmpService;
import landun.util.MD5;

import java.util.Map;

public class EmpAction extends ActionSupport {

    private final String SESSION_USER = "user";
    private EmpService empService;

    private SysEmployee employee;

    public String login(){
        SysEmployee emp = empService.findByName(employee.getSn());
        employee.setPassword(MD5.MD5Encode(employee.getPassword()));
        if(emp==null){
            return "login";
        }else{
            if(emp.getPassword().equals(employee.getPassword())){
                Map<String, Object> session = ActionContext.getContext().getSession();
                session.put(SESSION_USER,emp);
                String nameCn = emp.getPosition().getNameCn();
                session.put("employee_position",nameCn);
                return "ok";
            }else{
                return "login";
            }
        }
    }

    public String logout(){
        Map<String, Object> session = ActionContext.getContext().getSession();
        if(session.get(SESSION_USER)!=null){
            session.remove(SESSION_USER);
        }
        return "login";
    }

    public SysEmployee getEmployee() {
        return employee;
    }

    public void setEmployee(SysEmployee employee) {
        this.employee = employee;
    }

    public EmpService getEmpService() {
        return empService;
    }

    public void setEmpService(EmpService empService) {
        this.empService = empService;
    }
}
